package org.langqiao.io;

import java.util.ArrayList;
import java.util.List;

public class Prase {

	/**
	 * @param longitude 经度
	 * @param latitude 纬度
	 */
	public static List<Double> CalGuassFromLB(double longitude, double latitude) {
		int nCenterL = ((int) longitude / 6 + 1) * 6 - 3;
		List<Double> list = SubGussFs(longitude, latitude, nCenterL);
		return list;
	}

	/**
	 * @param longitude 经度
	 * @param latitude 纬度
	 * @param nCenterL 
	 * @return 
	 */
	private static List<Double> SubGussFs(double longitude, double latitude, int nCenterL) {
		List<Double> list = new ArrayList<Double>();
		// 高斯投影分带
		int nzonenum;
		if (nCenterL == 0) {
			nzonenum = (int) latitude / 6 + 1;
			nCenterL = nzonenum * 6 - 3;
		} else
			nzonenum = (int) nCenterL / 6 + 1;
		// 以弧度为单位的经纬度数值
		double rB = longitude / 180 * 3.1415926;
		double rL = (latitude - nCenterL) / 180 * 3.1415926; // 同时计算了中央经线
		// 1980坐标系参数
		final double a = 6378245.00; // 长轴
		final double b = 6356863.50; // 短轴
		double sqre1 = (a * a - b * b) / (a * a); // 第一偏心率平方
		// 子午圈曲率半径
		double sinb = Math.sin(rB);
		double cosb = Math.cos(rB);
		double M = a * (1 - sqre1) / (1 - sqre1 * sinb * sinb) / Math.sqrt(1 - sqre1 * sinb * sinb);
		// 卯酉圈曲率半径
		double N = a / Math.sqrt(1 - sqre1 * sinb * sinb);
		double sqrita = N / M - 1;

		// 该纬度点到赤道的子午线弧长
		double s = a * (1 - sqre1) * (1.00505117739 * rB - 0.00506237764 / 2 * Math.sin(2 * rB) + 0.0000106245 / 4 * Math.sin(4 * rB) - 0.00000002081 / 6 * Math.sin(6 * rB));

		double tanb = Math.tan(rB);

		double x = s + rL * rL * N / 2 * sinb * cosb + rL * rL * rL * rL * N / 24 * sinb * cosb * cosb * cosb * (5 - tanb * tanb + 9 * sqrita * sqrita + 4 * sqrita);
		double y = rL * N * cosb + rL * rL * rL * N / 6 * cosb * cosb * cosb * (1 - tanb * tanb + sqrita) + rL * rL * rL * rL * rL * N / 120 * cosb * cosb * cosb * cosb * cosb * (5 - 18 * tanb * tanb + tanb * tanb * tanb * tanb);
		y = y + 500000 + nzonenum * 1000000;
		list.add(x);
		list.add(y);
		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Double> list = Prase.CalGuassFromLB(121.462093, 31.341474);
		System.out.println(list.get(0));
		System.out.println(list.get(1));
	}

}
